from exts import db
from shortuuid import uuid
from werkzeug.security import generate_password_hash,check_password_hash

#定义用户模型
class FrontUser(db.Model):
    __tablename__ = 'front_user'
    id = db.Column(db.Integer,primary_key=True,autoincrement=True,default=uuid)
    nickname = db.Column(db.String(100),default='匿名用户')
    telephone = db.Column(db.String(11),nullable=False,unique=True)
    _password = db.Column(db.String(50),nullable=False)
    avarat = db.Column(db.String(100))
    desc = db.Column(db.TEXT)
    email = db.Column(db.String(50))

    def __init__(self,*args,**kwargs):
        if 'password' in kwargs:
            self.password = kwargs.get('password')
            kwargs.pop('password')
        super(FrontUser, self).__init__(*args,**kwargs)
    @property
    def password(self):
        return self._password

    @password.setter
    def password(self,newpwd):
        self.password = generate_password_hash(newpwd)

    def check_pwd(self,raw_pwd):
        return check_password_hash(self._password,raw_pwd)